package com.cyandpos.pad.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;


import com.cyandpos.pad.R;
import com.cyandpos.pad.activity.CardCouponPayActivity;
import com.cyandpos.pad.activity.CodedCouponActivity;
import com.cyandpos.pad.activity.MainActivity;
import com.cyandpos.pad.activity.PayActivity;
import com.cyandpos.pad.activity.PayCashCardActivity;
import com.cyandpos.pad.activity.RefundActivity;
import com.cyandpos.pad.activity.ScanCodeActivity;
import com.cyandpos.pad.activity.attachfunction.MemberCodedCouponQueryActivity;
import com.cyandpos.pad.cateringformat.CateringMainActivity;
import com.cyandpos.pad.cateringformat.choosemenu.MenuFoodActivity;
import com.cyandpos.pad.bankpay.P_BankData;
import com.cyandpos.pad.bankpay.bankImpl.BOC_BeiJing_Function;
import com.cyandpos.pad.common.commondata.Constant;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.commondata.MemberIdentificationMethodConstant;
import com.cyandpos.pad.common.commondata.SktConfigConstant;
import com.cyandpos.pad.common.commondata.SktConfigIdCrmConstant;
import com.cyandpos.pad.common.swipemanager.SwipeCardCommon;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.posdata.QueryMemberReq;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.cyandpos.pad.posdata.Sktconfig;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.log.LogUtils;

import java.util.Iterator;


/**
 * Created by Administrator on 2020/3/1.
 */

public class PayUtil {
    public static AlertDialog mDialog;//所有dialog
    public static String[] type;
    public static Payment createPayment(int id) {
        Payment paymentTemp = null;
        for (Payment e : ErpCommonData.loadConfig.getPayList()) {
            if (e.getPayCode() == id) {
                paymentTemp = e;
                break;
            }
        }
        if (paymentTemp == null)
            return null;
        Payment payMent = null;
        int type = paymentTemp.getPayType();
        payMent = new Payment(type);
        payMent.setPayCode(paymentTemp.getPayCode());
        payMent.setPayName(paymentTemp.getPayName());
        payMent.setYhqId(paymentTemp.getYhqId());
        payMent.setChangeType(paymentTemp.getChangeType());
        payMent.setXssx(paymentTemp.getXssx());
        return payMent;
    }

    public static boolean CheckPay(int id) {
        Iterator<Payment> iterator = ErpCommonData.ticketInfo.getPayList().iterator();
        while (iterator.hasNext()) {
            Payment pMent = iterator.next();
            if (pMent.getPayCode() == id) {
                iterator.remove();
                return true;
            }
        }
        return false;
    }

    public static void sortTicketPayments(TicketInfo ticket) {
        Payment paymentCash = null;
        for (Payment e : ticket.getPayList()) {
            if (e.getPayCode() == 1) {
                paymentCash = e;
                break;
            }
        }
        if (paymentCash == null)
            return;
        ticket.getPayList().remove(paymentCash);
        ticket.getPayList().add(0, paymentCash);
    }

    public static void paymentTypeHandle(int payType, int currentPay,PayActivity mContext) {
        if(payType == Payment.SKFS_ZFB || payType == Payment.SKFS_WX || payType == Payment.SKFS_JH){
            if(mContext.isWxOrZfBBL){
                mContext.reqAddpayNet(currentPay,false);// 同步付款 （确认支付调用）
            }else{
                Intent intent = new Intent(mContext, ScanCodeActivity.class);
                if(payType == Payment.SKFS_ZFB){
                    mContext.startActivityForResult(intent, Constant.PAY_ZFB_SCANCODE_RESULTID);
                }else if(payType == Payment.SKFS_JH){
                    mContext.startActivityForResult(intent, Constant.PAY_JH_SCANCODE_RESULTID);
                }
                else {
                    mContext.startActivityForResult(intent, Constant.PAY_WX_SCANCODE_RESULTID);
                }
            }
        }else if (payType == Payment.SKFS_YHK){
            if(CommonUtils.isAppInstalled(mContext, BOC_BeiJing_Function.packageName)){
                mContext.fastTradingProgress.show();
                P_BankData p_data = new P_BankData();
                p_data.setAmt(currentPay+"");
                new BOC_BeiJing_Function().handleConsume(p_data,mContext);
            }else{
                ToastUtils.showMsg("暂无此银行功能！");
            }

        }else{
            mContext.reqAddpayNet(currentPay,false);// 同步付款 （确认支付调用）
           // mContext.reqAddPayAndCommitNet(currentPay,false);//判读是否请求commit
        }
    }

    public static Boolean payParamCheck(int mSelectedPmtId, int payType, int currentPay, int saleType, int traceTotalMoney) {
        boolean checkFlag = true;
        if (mSelectedPmtId == -1 || payType == -1){
            ToastUtils.showMsg("请选择支付方式",1);
            checkFlag = false;
            return checkFlag;
        }
        if (currentPay <= 0) {
            ToastUtils.showMsg("请正确输入金额",1);
            checkFlag = false;
            return checkFlag;
        }
        if (saleType == 1) {//退货
            if (currentPay > traceTotalMoney) {
                ToastUtils.showMsg("退货金额大于销售金额！",1);
                checkFlag = false;
                return checkFlag;
            }
        }
        Payment payMent = createPayment(mSelectedPmtId);
        if (payMent == null){
            ToastUtils.showMsg("收款台没有此收款方式！",1);
            checkFlag = false;
            return checkFlag;
        }

        return checkFlag;
    }

    /**
     *会员识别方式
     * @param mContext
     * @param flag  1 卡券  2储值卡  3 编码券  4 返券后刷卡   5退货会员识别 6查询会员
     */
    public static void showQueryMemberDialog(final Activity mContext, final int flag) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_member, null);
        TextView tvName = view.findViewById(R.id.tv_dialog_title_name);
        final SelfNumKeyView sf_containt = view.findViewById(R.id.member_soft_keyboard);

        boolean isNeedInputView = false;//是否需要输入布局

        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON:
                tvName.setText("选择会员方式");
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:
                tvName.setText("选择方式");
                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON_QUERY:
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                tvName.setText("选择会员方式");
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:
                tvName.setText("选择会员方式");
                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                tvName.setText("选择会员方式");
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_REGISTER_MEMBER:
                tvName.setText("选择会员注册方式");
                break;
            default:
                tvName.setText("选择会员方式");
                break;
        }
        //关闭按钮
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                closeTypeDialog(mContext,flag);
            }
        });

        view.findViewById(R.id.tv_sale_member_swipe).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_scan).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_card).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_phone).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_idcard).setVisibility(View.GONE);

        final String memberList = handleMemberIdentificationList(flag).replace("(","").replace(")","");
        type=memberList.split(";");
        LogUtils.d("Member","字符串:"+memberList);

        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_SWIPE)){
            view.findViewById(R.id.tv_sale_member_swipe).setVisibility(View.VISIBLE);
        }
        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_SCAN)){
            view.findViewById(R.id.tv_sale_member_scan).setVisibility(View.VISIBLE);
        }
        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_CARDNO)){
            view.findViewById(R.id.tv_sale_member_card).setVisibility(View.VISIBLE);
            isNeedInputView = true;
        }
        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_PHONE_NUMBER)){
            view.findViewById(R.id.tv_sale_member_phone).setVisibility(View.VISIBLE);
            isNeedInputView = true;
        }
        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_IDENTIFICATION_NUMBER)){
            view.findViewById(R.id.tv_sale_member_idcard).setVisibility(View.VISIBLE);
            isNeedInputView = true;
        }
        if(!isNeedInputView)
            sf_containt.setVisibility(View.GONE);//不需要输入布局

        //会员刷卡
        view.findViewById(R.id.tv_sale_member_swipe).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                swipeCardtypeDialog(mContext,flag);
            }
        });
        //会员扫码
        view.findViewById(R.id.tv_sale_member_scan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
                scanCodeTypeDialog(mContext,flag);
            }
        });

        //卡号
        final EditText etCardNo = view.findViewById(R.id.et_sale_member_card);
        final TextView tvCardNo = view.findViewById(R.id.tv_sale_member_card);

        //手机号
        final EditText etPhone = view.findViewById(R.id.et_sale_member_phone);
        final TextView tvPhone = view.findViewById(R.id.tv_sale_member_phone);

        tvCardNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                tvCardNo.setVisibility(View.GONE);
                etCardNo.setVisibility(View.VISIBLE);

                if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_PHONE_NUMBER)) {
                    tvPhone.setVisibility(View.VISIBLE);
                    etPhone.setVisibility(View.GONE);
                }

                etPhone.setText("");
                etCardNo.setFocusable(true);
                sf_containt.setEnterReceptor(etCardNo);
                sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
                    @Override
                    public void keyBoardSure(String num,String hint) {
                        mDialog.dismiss();
                        ToastUtils.show("卡号开始查询会员");
                        //卡号事件
                        cardNoTypeDialog(mContext,flag,num);
                    }
                });
            }
        });
        CommonUtils.setNoSoftware(etCardNo, mContext,true);
        CommonUtils.setNoSoftware(etPhone, mContext,true);

        tvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvPhone.setVisibility(View.GONE);
                etPhone.setVisibility(View.VISIBLE);

                if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_CARDNO)) {
                    tvCardNo.setVisibility(View.VISIBLE);
                    etCardNo.setVisibility(View.GONE);
                }

                etCardNo.setText("");
                etPhone.setFocusable(true);
                sf_containt.setEnterReceptor(etPhone);
                sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
                    @Override
                    public void keyBoardSure(String num,String hint) {
                        mDialog.dismiss();
                        ToastUtils.show("手机号开始查询会员");
                        //手机号事件
                        phoneTypeDialog(mContext,flag,num);
                    }
                });

            }
        });
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();
    }

    private static String handleMemberIdentificationList(int flag) {
        String memberType="";
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_CASH_COUPON_QUREY);
                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON_QUERY:
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_COUPON);
                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_CASH_COUPON_QUREY);
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_CASH_CARD);
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_GRANT_COUPON);
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_ADD_MEMBER:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_INFO);
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_REGISTER_MEMBER:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_INFO);
                break;
            default:
                break;

        }
        return memberType;
    }

    private static String GetMemberIdentifyingList(String strFlag) {
        String memberType="";
        for(Sktconfig item: ErpCommonData.loadConfig.getSkt().getSktconfigList()){
            if(item.getLx().equals(SktConfigConstant.SKT_CONFIG_TYPE_CRM)){
                if(item.getId().equals(strFlag) ){
                     memberType = item.getValue();
                     break;
                }
            }
        }
        return memberType;
    }

    /**
     *  卡号
     * @param mContext
     * @param flag
     * @param content
     */
    private static void cardNoTypeDialog(Activity mContext, int flag, String content) {
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON:
                ((CardCouponPayActivity) mContext).QuickResult(content, SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_CARD);
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:
                ((PayCashCardActivity) mContext).QuickResult(content, SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_CARD+"");
                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON_QUERY:
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                QueryMemberRequest data1 = new QueryMemberRequest();
                data1.setServerBillId(Integer.parseInt(ErpCommonData.ticketInfo.getServerBillId()));
                data1.setCodedCouponType(1);
                data1.setSktno(ErpCommonData.sktno);
                data1.setCondType(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_CARD);
                data1.setCondValue(content);
                if(flag == Constant.MEMBER_IDENTIFYING_CODECOUPON_QUERY)
                    ((MemberCodedCouponQueryActivity)mContext).mPresenter.getCodedCouponList(data1);
                else
                    ((CodedCouponActivity)mContext).mPresenter.getCodedCouponList(data1);
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:
                QueryMemberRequest requestParam = new QueryMemberRequest();
                requestParam.setServerBillId(Integer.parseInt(ErpCommonData.ticketInfo.getServerBillId()));
                requestParam.setCondType(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_CARD);
                requestParam.setCondValue(content);
                requestParam.setSktno(ErpCommonData.sktno);
                ((PayActivity)mContext).netPresenter.getVipCardToGrantVoucher(requestParam);
                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                ((RefundActivity)mContext).queryMemberReturn(content,SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_CARD+"");
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_ADD_MEMBER:
                QueryMemberReq memberReq = new QueryMemberReq();
                memberReq.setCondType(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_CARD+"");
                memberReq.setCardNo(content);
                memberReq.setOrderNo(ErpCommonData.ticketInfo.getOrderNo());
                memberReq.setStoreCode(ErpCommonData.storeCode);
                if(mContext instanceof MainActivity) {
                    ((MainActivity) mContext).mSalePresenterImpl.queryMember(memberReq);
                }else if(mContext instanceof CateringMainActivity){
                    ((CateringMainActivity) mContext).queryMember(memberReq);
                }else if(mContext instanceof MenuFoodActivity){
                    ((MenuFoodActivity) mContext).queryMember(memberReq);
                }
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_REGISTER_MEMBER:
                QueryMemberReq memberReq1 = new QueryMemberReq();
                memberReq1.setIsCreate("1");
                memberReq1.setCondType(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_CARD+"");
                memberReq1.setCardNo(content);
                memberReq1.setOrderNo(ErpCommonData.ticketInfo.getOrderNo());
                memberReq1.setStoreCode(ErpCommonData.storeCode);
                if(mContext instanceof MainActivity) {
                    ((MainActivity) mContext).mSalePresenterImpl.queryMember(memberReq1);
                }else if(mContext instanceof CateringMainActivity){
                    ((CateringMainActivity) mContext).queryMember(memberReq1);
                }else if(mContext instanceof MenuFoodActivity){
                    ((MenuFoodActivity) mContext).queryMember(memberReq1);
                }
                break;
            default:
                break;

        }
    }

    /**
     * 手机号
     *  @param mContext
     * @param flag 1卡券  2储值卡  3 编码券  4 后返券刷卡   5退货会员识别
     * @param content
     */
    private static void phoneTypeDialog(Activity mContext, int flag, String content) {
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON:
                ((CardCouponPayActivity) mContext).QuickResult(content, SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_PHONE);
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:
                ((PayCashCardActivity) mContext).QuickResult(content, SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_PHONE+"");
                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON_QUERY:
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                QueryMemberRequest data1 = new QueryMemberRequest();
                data1.setServerBillId(Integer.parseInt(ErpCommonData.ticketInfo.getServerBillId()));
                data1.setCondType(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_PHONE);
                data1.setCodedCouponType(1);
                data1.setSktno(ErpCommonData.sktno);
                data1.setCondValue(content);
                if(flag == Constant.MEMBER_IDENTIFYING_CODECOUPON_QUERY)
                    ((MemberCodedCouponQueryActivity)mContext).mPresenter.getCodedCouponList(data1);
                else
                    ((CodedCouponActivity)mContext).mPresenter.getCodedCouponList(data1);
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:
                QueryMemberRequest requestParam = new QueryMemberRequest();
                requestParam.setServerBillId(Integer.parseInt(ErpCommonData.ticketInfo.getServerBillId()));
                requestParam.setCondType(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_PHONE);
                requestParam.setCondValue(content);
                requestParam.setSktno(ErpCommonData.sktno);
                ((PayActivity)mContext).netPresenter.getVipCardToGrantVoucher(requestParam);
                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                QueryMemberReq memberReq = new QueryMemberReq();
                memberReq.setCondType(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_PHONE+"");
                memberReq.setCardNo(content);
                memberReq.setOrderNo( ((RefundActivity)mContext).ticket.getOrderNo());
                memberReq.setStoreCode(ErpCommonData.storeCode);
                ((RefundActivity)mContext).mPresenter.getVipInfo(memberReq);
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_ADD_MEMBER:
                QueryMemberReq memberR = new QueryMemberReq();
                memberR.setCondType(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_PHONE+"");
                memberR.setCardNo(content);
                memberR.setOrderNo(ErpCommonData.ticketInfo.getOrderNo());
                memberR.setStoreCode(ErpCommonData.storeCode);
                if(mContext instanceof MainActivity) {
                    ((MainActivity) mContext).mSalePresenterImpl.queryMember(memberR);
                }else if(mContext instanceof CateringMainActivity){
                    ((CateringMainActivity) mContext).queryMember(memberR);
                }else if(mContext instanceof MenuFoodActivity){
                    ((MenuFoodActivity) mContext).queryMember(memberR);
                }
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_REGISTER_MEMBER:
                QueryMemberReq memberR1 = new QueryMemberReq();
                memberR1.setIsCreate("1");
                memberR1.setCondType(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_TYPE_PHONE+"");
                memberR1.setCardNo(content);
                memberR1.setOrderNo(ErpCommonData.ticketInfo.getOrderNo());
                memberR1.setStoreCode(ErpCommonData.storeCode);
                if(mContext instanceof MainActivity) {
                    ((MainActivity) mContext).mSalePresenterImpl.queryMember(memberR1);
                }else if(mContext instanceof CateringMainActivity){
                    ((CateringMainActivity) mContext).queryMember(memberR1);
                }else if(mContext instanceof MenuFoodActivity){
                    ((MenuFoodActivity) mContext).queryMember(memberR1);
                }
                break;
            default:
                break;

        }
    }

    /**
     * 扫码
     * @param mContext
     * @param flag
     */
    private static void scanCodeTypeDialog(Activity mContext, int flag) {
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON:
                Intent intent = new Intent(mContext,ScanCodeActivity.class);
                mContext.startActivityForResult(intent,  Constant.YHQ_SCAN_HANDLEID);
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:
                Intent intent2 = new Intent(mContext, ScanCodeActivity.class);
                mContext.startActivityForResult(intent2,  Constant.CZK_SCAN_HANDLEID);
                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON_QUERY:
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                Intent intent3 = new Intent(mContext,ScanCodeActivity.class);
                mContext.startActivityForResult(intent3,  Constant.BMQ_SCAN_HANDLEID);
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:
                Intent intent4 = new Intent(mContext,ScanCodeActivity.class);
                mContext.startActivityForResult(intent4,  Constant.PAY_AFTER_SCAN_HANDLEID);
                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                Intent intent5 = new Intent(mContext,ScanCodeActivity.class);
                mContext.startActivityForResult(intent5,  Constant.RETURN_SCAN_HANDLEID);
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_ADD_MEMBER:
                Intent intent6 = new Intent(mContext,ScanCodeActivity.class);
                mContext.startActivityForResult(intent6,  Constant.BMQ_SCAN_MEMBER);
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_REGISTER_MEMBER:
                Intent intent7 = new Intent(mContext,ScanCodeActivity.class);
                mContext.startActivityForResult(intent7,  Constant.BMQ_SCAN_REGISTER_MEMBER);
                break;
            default:
                break;

        }
    }

    /**
     * 刷卡
     * @param mContext
     * @param flag 1卡券  2储值卡  3 编码券  4后返券刷卡   5退货会员识别
     */
    private static void swipeCardtypeDialog(Activity mContext, int flag) {
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON:
                SwipeCardCommon swipeCard = new SwipeCardCommon(mContext);
                swipeCard.setReceptor((CardCouponPayActivity)mContext, Constant.YHQ_SWIPECARD_HANDLEID);
                swipeCard.startSwipe();
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:
                SwipeCardCommon swipeCard2 = new SwipeCardCommon(mContext);
                swipeCard2.setReceptor((PayCashCardActivity)mContext, Constant.CZK_SWIPECARD_HANDLEID);
                swipeCard2.startSwipe();
                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                SwipeCardCommon swipeCard3 = new SwipeCardCommon(mContext);
                swipeCard3.setReceptor((CodedCouponActivity)mContext, Constant.BMQ_SWIPECARD_HANDLEID);
                swipeCard3.startSwipe();
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:
                SwipeCardCommon swipeCard4 = new SwipeCardCommon(mContext);
                swipeCard4.setReceptor((PayActivity)mContext, Constant.PAY_AFTER_SWIPECARD_HANDLEID);
                swipeCard4.startSwipe();
                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                SwipeCardCommon swipeCard5 = new SwipeCardCommon(mContext);
                swipeCard5.setReceptor((RefundActivity)mContext, Constant.REFUND_SWIPECARD_HANDLEID);
                swipeCard5.startSwipe();
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_ADD_MEMBER:
                SwipeCardCommon swipeCard6 = new SwipeCardCommon(mContext);
                swipeCard6.setReceptor((MainActivity)mContext, Constant.MEMBER_SWIPECARD_HANDLEID);
                swipeCard6.startSwipe();
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_REGISTER_MEMBER:
                SwipeCardCommon swipeCard7 = new SwipeCardCommon(mContext);
                swipeCard7.setReceptor((MainActivity)mContext, Constant.MEMBER_SWIPECARD_REGISTER);
                swipeCard7.startSwipe();
                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON_QUERY:
                SwipeCardCommon swipeCard8 = new SwipeCardCommon(mContext);
                swipeCard8.setReceptor((MemberCodedCouponQueryActivity)mContext, Constant.BMQ_SWIPECARD_HANDLEID);
                swipeCard8.startSwipe();
                break;
            default:
                break;

        }
    }

    /**
     *  关闭
     * @param mContext
     * @param flag  1卡券  2储值卡  3 编码券  4后返券刷卡   5退货会员识别
     */
    private static void closeTypeDialog(Activity mContext, int flag) {
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON://不需处理
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:
                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:
                ((PayActivity)mContext).netPresenter.saveCommitPayRequest(((PayActivity)mContext).mTicket,true);
                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                break;
            default:
                break;

        }
    }

    /**
     * 软键盘自动弹出且光标定位到EditText默认值后
     * @param et
     * @param mContext
     */
    public static void showSoftKeyboard(final EditText et,final Activity mContext) {
        CharSequence text = et.getText();
        if (text instanceof Spannable) {
            Spannable spanText = (Spannable) text;
            Selection.setSelection(spanText, text.length());
        }
        et.post(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                et.setFocusable(true);
                et.setFocusableInTouchMode(true);
                et.requestFocus();
            }
        });

    }

    /**
     * 判断交易是否是退货交易
     * @param mTicket
     * @return true 退货     false 销售
     */
    public static  boolean  isReturnedTrade(TicketInfo mTicket){
        boolean isReturned = false;
        if(!TextUtils.isEmpty(mTicket.getOldOrderNo()) && mTicket.getOldOrderNo().length()>0){
            isReturned = true;
        }
        return isReturned;
    }


}
